﻿using System;
using System.IO;
using System.Web.Security;
using EmptyWallGallery.Core;
using EmptyWallGallery.Core.Utilities;

namespace EmptyWallGallery.FrontEnd.inside.Tools
{
    public partial class SlideShowUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            if (Request.QueryString["ID"] == null) return;
            if (!Common.Instance.IsNumeric(Request.QueryString["ID"])) return;

            GetSlide(Convert.ToInt32(Request.QueryString["ID"]));
        }

        public void GetSlide(int slideId)
        {
            var item = Core.BLL.SlideShow.Instance.GetById(slideId);
            if (item == null) return;

            var membershipUser = Membership.GetUser(new Guid(item.CreateBy));
            if (membershipUser != null)
                txtCreateBy.Text = membershipUser.UserName;
            txtCreateDate.Text = Convert.ToString(item.CreateDate);
            txtDesc.Text = item.ImageDesc;
            txtImgUrl.Value = item.ImageUrl;
            DrpStatus.SelectedValue = Convert.ToString(item.Status);
            ImgContent.ImageUrl = Configs.HomeUrl + "Uploads/" + item.ImageUrl.Replace("130x0__", "486x0__");
        }

        protected void BtnUpdateOnClick(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["ID"] == null) return;
                if (!Common.Instance.IsNumeric(Request.QueryString["ID"])) return;

                var membershipUser = Membership.GetUser();
                if (membershipUser == null) return;
                if (membershipUser.ProviderUserKey == null) return;
                var strImg = txtImgUrl.Value;
                var strDesc = Common.Instance.NoHtml(txtDesc.Text);
                var status = Convert.ToBoolean(DrpStatus.SelectedValue);

                if (fileUpload.HasFile)
                {
                    if (!Common.Instance.IsValidFile(fileUpload.PostedFile, new[] { "png", "gif", "jpg" })) return;
                    var randomStr = new RandomStrings();
                    var strFileName = randomStr.GenerateRandomString(DateTime.Now) + ".jpg";
                    var strFolder = string.Format("{0}{1}\\{2}\\{3}\\{4}\\", Configs.UserFolder,
                                                  membershipUser.UserName, DateTime.Now.Year,
                                                  Common.Instance.Right("0" + DateTime.Now.Month, 2),
                                                  Common.Instance.Right("0" + DateTime.Now.Day, 2));

                    if (!Directory.Exists(strFolder))
                    {
                        Directory.CreateDirectory(strFolder);
                    }

                    var objImage = new ImageUtil(fileUpload.PostedFile.InputStream, 1024, 1024, "UploadService",
                                                 membershipUser.UserName,
                                                 strFileName, 80);

                    objImage.PublicUpload(130, 0, strFileName, 100, "", strFolder);
                    objImage.PublicUpload(486, 0, strFileName, 100, "", strFolder);

                    strImg = strFolder.Replace(Configs.UserFolder, "").Replace("\\", "/") + "130x0__" + strFileName;
                }

                if (
                    !Core.BLL.SlideShow.Instance.Update(strDesc, strImg, status,
                                                        membershipUser.ProviderUserKey.ToString(),
                                                        Convert.ToInt32(Request.QueryString["ID"])))
                {
                }
                else
                {
                    if (Core.BLL.SlideShow.Instance.WriteSlideItem())
                    {
                        Response.Redirect("SlideShow.aspx", false);
                    }
                }
            }
            catch (Exception exception)
            {
            }
        }
    }
}